<?php 
defined('RUN_KATANA') or die('Restricted access');
if($user->getId() > 0)
{ $view = 'customer'; }
else
{ $view = 'new_user'; }

switch ($_REQUEST['action'])
{
	case 'new_user_quick':
		$view = 'new_user';
		$forms['new_user_quick']=$ac->getForms('new_user_quick');
		$forms['new_user_quick']->validate($txt, $com);
		if($forms['new_user_quick']->Valid)
		{
			$usr['id'] = $user->findUserId('', $forms['new_user_quick']->Fields['email']->Value);
			if($usr['id'] == 0)
			{ $usr['id'] = $user->findUserId($forms['new_user_quick']->Fields['email']->Value); }
			if($usr['id'] == 0)
			{
				$user->registerUser($forms['new_user_quick']->Fields['email']->Value, $forms['new_user_quick']->Fields['password2']->Value, $forms['new_user_quick']->Fields['email']->Value); 
				$usr['id'] = $user->findUserId($forms['new_user_quick']->Fields['email']->Value);
				if($usr['id'] > 0)
				{ 
					$cart->SetUserID($com, $usr['id']); 
					if(!USER_ACTIVATE)
					{ 
						$user->loginUser($forms['new_user_quick']->Fields['email']->Value, $forms['new_user_quick']->Fields['password2']->Value, false); 
						$view = 'customer';
					}
				}
			}
			else
			{ $forms['new_user_quick']->Fields['email']->Error = 'Email allready taken'; }
		}
		break;
	case 'customer':
		$view = 'customer';
		if(!($userDetails))
		{ 
			$userDetails = new UserDetails($user); 
			$userDetails->LoadDetails();
		}
		
		$forms['customer']=$ac->getForms('customer');
		$forms['customer']->validate($txt, $com);
		
		$forms['alternate_delivery']=$ac->getForms('alternate_delivery');
		
		if($forms['customer']->Valid)
		{
			$userDetails->setName($forms['customer']->Fields['name']->getValue());
			$userDetails->setLastname($forms['customer']->Fields['lastname']->getValue());
			$userDetails->setCompany($forms['customer']->Fields['company']->getValue());
			$userDetails->setAddress($forms['customer']->Fields['address']->getValue());
			$userDetails->setAddress2($forms['customer']->Fields['address2']->getValue());
			$userDetails->setZip($forms['customer']->Fields['zip']->getValue());
			$userDetails->setCity($forms['customer']->Fields['city']->getValue());
			$userDetails->setCountry($forms['customer']->Fields['country']->getValue());
			$userDetails->setPhone($forms['customer']->Fields['phone']->getValue());
			if($forms['customer']->Fields['email']->getValue() != $userDetails->getEmail())
			{
				$usr['id'] = 0;
				$usr['id'] = $user->findUserId('', $forms['customer']->Fields['email']->getValue());
				if($usr['id'] == 0)
				{ $usr['id'] = $user->findUserId($forms['customer']->Fields['email']->getValue()); }
				if(($usr['id'] == 0) || ($usr['id'] == $userDetails->getId()))
				{
					$userDetails->setEmail($forms['customer']->Fields['email']->getValue());
				}
				else
				{ 
					$forms['customer']->Fields['email']->Error = 'Email address allready in use'; 
					$forms['customer']->Fields['email']->setValue($userDetails->getEmail());
				}
			}
			
			if($userDetails->getAlternateDelivery() > 0)
			{
				$userDetails->setAlternateDelivery($forms['customer']->Fields['alternate_delivery']->Value);
				$forms['alternate_delivery']->validate($txt, $com);
				if($forms['alternate_delivery']->Valid)
				{
					$userDetails->setDeliveryName($forms['alternate_delivery']->Fields['delivery_name']->getValue());
					$userDetails->setDeliveryLastName($forms['alternate_delivery']->Fields['delivery_lastname']->getValue());
					$userDetails->setDeliveryAddress($forms['alternate_delivery']->Fields['delivery_address']->getValue());
					$userDetails->setDeliveryAddress2($forms['alternate_delivery']->Fields['delivery_address2']->getValue());
					$userDetails->setDeliveryZip($forms['alternate_delivery']->Fields['delivery_zip']->getValue());
					$userDetails->setDeliveryCity($forms['alternate_delivery']->Fields['delivery_city']->getValue());
					$userDetails->setDeliveryCountry($forms['alternate_delivery']->Fields['delivery_country']->getValue());
					$userDetails->setValid_customer(1);
					$userDetails->saveDetails();
					$view = 'customer';
				}
			}
			else
			{
				$userDetails->setValid_customer(1);
				$userDetails->saveDetails();
				$view = 'customer';
			}
		}
		break;
}
?>